package com.lxg.service.userService.impl;

import com.lxg.dao.BooksDao;
import com.lxg.dao.BorrowDao;
import com.lxg.dao.HistoryDao;
import com.lxg.dao.UserDao;
import com.lxg.pojo.Books;
import com.lxg.pojo.Borrow;
import com.lxg.pojo.History;
import com.lxg.pojo.User;
import com.lxg.service.userService.UserBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Service
public class UserBorrowServiceImpl implements UserBorrowService {
    @Autowired
    UserDao userDao;
    @Autowired
    HistoryDao historyDao;
    @Autowired
    BooksDao booksDao;

    @Autowired
    BorrowDao borrowDao;



    @Override
    public boolean updateUser(String username, String password, String name, String sno, String grade, String email, String phone,Integer uid) {
       /* User user1 = userDao.checkUsername(username);
        User user2 = userDao.checkEmail(email);
        User user3 = userDao.checkSno(sno);
        //判断想要修改的username和email有没有和数据库中的重复
        if(user1==null && user2==null && user3==null) {*/
            userDao.updateUser(username, password, name, sno, grade, email, phone,uid);
        return true;
        /*}else {
            return false;
        }*/
    }

    @Override
    public boolean ReturnBookService(Integer uid, Integer bid) {
        return false;
    }

    /**
     * 还书操作
     * @param uid
     * @param bid
     * @return
     */
    /*@Override
    public boolean ReturnBookService(Integer uid, Integer bid) {
        //查询该本被借的书
        Books book = booksDao.getBookByBid(bid);
        //查询该用户是否借了该书
        History history = historyDao.getHistoryByUidAndBid(uid, bid);
        //获取该记录状态
        Integer status = history.getStatus();

        //获取图书剩余数量
        Integer remainder = book.getRemainder();

        //获取当前时间
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        String datetime = simpleDateFormat.format(date);

        //进行判断
        if(status==0){
            //修改图书数量
            booksDao.updateBookRemainder(bid,remainder+1);
            //修改一条记录
            historyDao.updateHistory(uid,bid,datetime);
            return true;
        }else if(status==1){
            System.out.println("该书已经还!!!");
            return false;
        }else {
            System.out.println("没借过这本书!!!");
            return false;
        }

    }*/

    /**
     * 借书操作
     * @param uid
     * @param bid
     * @return
     */
    @Override
    public boolean BorrowBookService(Integer uid, Integer bid) {
        //查询该用户
        User user = userDao.getUserByUid(uid);
        //查询该本被借的书
        Books book = booksDao.getBookByBid(bid);
        //查询该用户是否已经借了这本书，若已经借了则不允许再借
        History history = historyDao.getHistoryByUidAndBid(uid, bid);
        //若借了看是否已经还
        Integer status = history.getStatus();

        //获取图书和用户名字
        String userName = user.getName();
        String bookName = book.getName();

        //获取用户和书本的times
        Integer userTimes = user.getTimes();
        Integer bookTimes = book.getTimes();

        //获取借阅图书的数量
        Integer bookNum = book.getNum();
        Integer remainder = book.getRemainder();




        //获取当前时间
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        String datetime = simpleDateFormat.format(date);
        //进行判断
        if(bookNum>0 && (history==null || status==1)){
            //修改书本的剩余数量
            booksDao.updateBookRemainder(bid,remainder+1);
            //修改借阅次数
            booksDao.updateBookTimes(bid, bookTimes+1);
            userDao.updateUserTimes(uid, userTimes+1);
            //添加一条记录
//            historyDao.addHistory(uid,bid,bookName,userName,datetime,null,0);
            return true;
        }else if(bookNum==0){
            System.out.println("图书数量不足!!!");
            return false;
        }else if(history!=null){
            System.out.println("已经借了该书啦!!!");
            return false;
        }else {
            return false;
        }
    }


    //添加借阅记录
    @Override
    public int addHistory(Integer uid, Integer bid, String bookname, String author, String begintime, String endtime,Integer num, Integer status) {
        int i = historyDao.addHistory(uid, bid, bookname, author, begintime, endtime,num, status);
        return i;
    }





    //根据uid和bid查找借阅记录

    @Override
    public History getHistoryByUidAndBid(Integer uid, Integer bid) {
        History historyByUidAndBid = historyDao.getHistoryByUidAndBid(uid, bid);
        return historyByUidAndBid;
    }





    @Override
    public ArrayList<Borrow> showMyBooks(Integer uid) {
        ArrayList<Borrow> allMyBooks = borrowDao.getAllMyBooks(uid);
        return allMyBooks;
    }

    @Override
    public boolean addMyBook(Integer bid,Integer uid, String bookname, String author, Integer num, Integer status) {
        boolean b = borrowDao.addMyBook(bid,uid, bookname, author, num, status);
        return  b;
    }

    @Override
    public Borrow getBorrow(Integer bid,Integer uid) {
        Borrow borrow = borrowDao.getBorrow(bid,uid);
        return borrow;
    }

    @Override
    public boolean updateBorrow(Integer bid,Integer uid, Integer num) {
        boolean b = borrowDao.updateBorrow(bid,uid,num);
        return b;
    }

    @Override
    public boolean deleteBorrow(Integer bid,Integer uid) {
        boolean b = borrowDao.deleteBorrow(bid,uid);
        return b;
    }



    //更新借阅记录
    @Override
    public boolean updateHistory(Integer uid, Integer bid, String endtime,Integer num) {
        historyDao.updateHistory(uid,bid,endtime, num);
        return true;
    }

    @Override
    public boolean deleteHistoryByUidAndBid(Integer uid, Integer bid) {
        boolean b = historyDao.deleteHistory(uid, bid);
        return b;
    }

    @Override
    public Integer isDeleted(Integer uid, Integer bid) {
        System.out.println("uid="+uid);
        System.out.println("bid="+bid);

        Integer deleted = borrowDao.isDeleted(uid, bid);
//        System.out.println("deleted: " + deleted);
        return deleted;
    }

    @Override
    public boolean updateHistoryStatus(Integer uid, Integer bid) {
        boolean b = historyDao.updateHistoryStatus(uid, bid);
        return b;
    }

    @Override
    public boolean updateBorrowStatus(Integer bid, Integer uid) {
        boolean b = borrowDao.updateBorrowStatus(bid,uid);
        return b;
    }

    @Override
    public Integer getHistoryStatus(Integer uid, Integer bid) {
        Integer historyStatus = historyDao.getHistoryStatus(uid, bid);
        return historyStatus;
    }


}
